package com.qsmx.store.app.main.display;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Presentation;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.core.BasePresentation;
import com.qsmx.store.core.fusion.FusionCode;
import com.qsmx.store.core.util.video.MediaVideoPlayerUtil;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@SuppressLint("NewApi")
public class DefaultDisplay extends BasePresentation implements SurfaceHolder.Callback, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener {
    private MediaPlayer mMediaPlayer;
    private SurfaceHolder surfaceHolder;
    private String videoPath;

    private SurfaceView svVideo;

    public DefaultDisplay(Context outerContext, Display display) {
        super(outerContext, display);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.display_default);
        initView();
        initVideoPath();
    }

    private void initView() {
        svVideo = findViewById(R.id.sv_video);
    }

    private void initVideoPath() {

        final File filepath = new File(FusionCode.SAVE_PATH + "video");
        if (!filepath.exists()) {
            filepath.mkdirs();
        }

        File videoFile = new File(FusionCode.VIDEO_PATH + "top.mp4");
        if (!videoFile.exists()) {
            showShortToast(getContext(), "视频文件不存在，请拷贝视频文件至根目录-fyzq-video文件夹中");
        } else {
            videoPath = videoFile.getPath();
            initVideo();
        }

    }

    private void initVideo() {
        surfaceHolder = svVideo.getHolder();
        surfaceHolder.addCallback(this);

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mMediaPlayer.seekTo(0);
        mMediaPlayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mediaPlayer) {
                mMediaPlayer.start();
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        MediaVideoPlayerUtil.loadSrc(videoPath, mMediaPlayer, surfaceHolder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    @Override
    public void onDisplayRemoved() {
        super.onDisplayRemoved();
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }
}
